home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Add-Ons / After Dark / ScreenFlip 1.5 / Source / AD Shared Resources / HandyStuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-05  |  4.4 KB  |  212 lines  |  [TEXT/R*ch]

  1. /* HandyStuff.c -- Various useful functions
  2. */
  3.  
  4. #include "HandyStuff.h"
  5.  
  6.  
  7. short
  8. RangedRdm (short min, short max)
  9. /* assume that min is less than max */
  10. {
  11.     long range, t;
  12.  
  13.     
  14.     range = max - min + 1;
  15.     t = (Random()&0x7fff) % range;
  16.     
  17.     if (t+min > max)
  18.     {    
  19.         SysBeep(2); SysBeep(2); SysBeep(2);
  20.     }
  21.     return( t+min );
  22. }
  23.  
  24.  
  25.     // This function tries to allocate a handle from temporary memory
  26.     // first, and only if that fails from the reserved memory.
  27.     // This function is taken from the DarkSide of the Mac example code.
  28.     // Note that the return value can be nil and should be checked.
  29. Handle
  30. BestNewHandle (Size s)
  31. {
  32.     Handle theHandle;
  33.     OSErr  anErr;
  34.     
  35.     if ((theHandle = TempNewHandle(s, &anErr)) == nil)
  36.         theHandle = NewHandle(s);
  37.         
  38.     return(theHandle);
  39. }
  40.  
  41.     // Whew, finally a support function I wrote all by myself!
  42.     // This next function is like the ToolBox call PtInRect, but works directly
  43.     // with two x/y values instead of with a Point structure.
  44.     
  45. Boolean
  46. XYInRect (Rect *r, short beeX, short beeY)
  47. {
  48.     return (beeX > r->left && beeX < r->right && beeY > r->top && beeY < r->bottom);
  49. }
  50.  
  51.  
  52.     // Draws a string at specified coordinates. Duh.
  53. void
  54. DrawStringAt (short x, short y, Str255 str)
  55. {
  56.     MoveTo(x, y);
  57.     DrawString(str);
  58. }
  59.  
  60.  
  61.     // Slightly more interesting: draws a string at the given
  62.     // y-coordinate, centered horizontally in the current port.
  63. void
  64. CenterString (short y, StringPtr str)
  65. {
  66.     GrafPtr port;
  67.     short left, right, width;
  68.     
  69.     GetPort(&port);
  70.     width = TextWidth(str, 1, *str);
  71.     left  = port->portRect.left;
  72.     right = port->portRect.right;
  73.  
  74.     DrawStringAt(left+(((right-left)-width)/2), y, str);
  75. }
  76.  
  77.  
  78.     // Adjusts a rectangle so that it is centered horizontally across
  79.     // the port, with the vertical mid-line at the given y-coordinate.
  80. void
  81. CenterRectHorizontal (Rect *r, short y)
  82. {
  83.     GrafPtr port;
  84.     short left, right, width, height;
  85.     
  86.     GetPort(&port);
  87.     width = r->right - r->left;
  88.     height = r->bottom - r->top;
  89.     left  = port->portRect.left;
  90.     right = port->portRect.right;
  91.  
  92.     r->left = left+(((right-left)-width)/2);
  93.     r->right = r->left + width;
  94.     r->top = y - height/2;
  95.     r->bottom = r->top + height;
  96. }
  97.  
  98.  
  99.     // Adjusts a rectangle so that it is centered vertically across
  100.     // the port, with the horizontal mid-line at the given x-coordinate.
  101. void
  102. CenterRectVertical (Rect *r, short x)
  103. {
  104.     GrafPtr port;
  105.     short top, bottom, width, height;
  106.     
  107.     GetPort(&port);
  108.  
  109.     width = r->right - r->left;
  110.     height = r->bottom - r->top;
  111.     top  = port->portRect.top;
  112.     bottom = port->portRect.bottom;
  113.  
  114.     r->top = top+(((bottom-top)-height)/2);
  115.     r->bottom = r->top + height;
  116.     r->left = x - width/2;
  117.     r->right = r->left + width;
  118. }
  119.  
  120.  
  121.     // Adjusts a rectangle so that it is centered horizontally and vertically 
  122.     // around the given x and y coordinates.
  123. void
  124. CenterRect (Rect *r, short x, short y)
  125. {
  126.     GrafPtr port;
  127.     short top, bottom, left, right, width, height;
  128.     
  129.     GetPort(&port);
  130.     width = r->right - r->left;
  131.     height = r->bottom - r->top;
  132.     left  = port->portRect.left;
  133.     right = port->portRect.right;
  134.     top  = port->portRect.top;
  135.     bottom = port->portRect.bottom;
  136.  
  137.     r->left = x - width/2;
  138.     r->right = r->left + width;
  139.     r->top = y - height/2;
  140.     r->bottom = r->top + height;
  141. }
  142.  
  143.  
  144.     // I am a very, very, lazy person.
  145. void
  146. LineFromTo (short xFrom, short yFrom, short xTo, short yTo)
  147. {
  148.     MoveTo(xFrom, yFrom);
  149.     LineTo(xTo, yTo);
  150. }
  151.  
  152.  
  153.     // Returns a random true/false value.
  154. Boolean
  155. RandomBool (void)
  156. {
  157.     return (RangedRdm(0,1) == 1);
  158. }
  159.  
  160.  
  161.     // This function stolen from the Glypha III source code.
  162. void
  163. RedAlert (StringPtr theStr)
  164. {
  165.     #define        kRedAlertID        128
  166.     short        whoCares;
  167.     
  168.     ParamText(theStr, "\p", "\p", "\p");
  169.     whoCares = StopAlert(kRedAlertID, 0L);
  170. }
  171.  
  172. // The following code was stolen from the NewsWatcher source code.
  173. /*----------------------------------------------------------------------------
  174.     SetPortTextStyle 
  175.     
  176.     Set the font, size, and style of the current port.
  177.             
  178.     Entry:    *style = text style record.
  179. ----------------------------------------------------------------------------*/
  180.  
  181. void
  182. SetPortTextStyle (TextStyle *style)
  183. {
  184.     TextFont(style->tsFont);
  185.     TextFace(style->tsFace);
  186.     TextSize(style->tsSize);
  187. }
  188.  
  189.  
  190.  
  191. /*----------------------------------------------------------------------------
  192.     GetPortTextStyle 
  193.     
  194.     Get the font, size, and style of the current port.
  195.             
  196.     Exit:    *style = text style record.
  197. ----------------------------------------------------------------------------*/
  198.  
  199. void 
  200. GetPortTextStyle (TextStyle *style)
  201. {    
  202.     GrafPtr port;
  203.     
  204.     GetPort(&port);
  205.  
  206.     style->tsFont = port->txFont;
  207.     style->tsFace = port->txFace;
  208.     style->tsSize = port->txSize;
  209.     
  210. }
  211.  
  212.